package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.domain.Brand;
import com.spzx.product.service.IBrandService;
import com.spzx.product.service.impl.BrandServiceImpl;
import com.spzx.vo.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/brand")
public class BrandController {



    @Autowired
    private BrandServiceImpl brandService;

    /**
     * 新增品牌
     */
    @Operation(summary = "新增品牌")
    @PostMapping
    public Result addBrand(@RequestBody Brand brand){
        boolean save = brandService.saveOrUpdate(brand);

        return Result.build(save,200,"新增品牌成功");
    }



    /**
     * 修改品牌
     * @param brand
     * @return
     */
    @Operation(summary = "修改品牌")
    @PutMapping
    public Result updateBrand(@RequestBody Brand brand){
        boolean b = brandService.updateById(brand);
        return Result.build(b,200,"新增品牌成功");
    }

    @Operation(summary ="获取品牌的详细信息")
    @GetMapping("/{id}")
    public Result<Brand> getBrand(@PathVariable("id") @RequestParam Long id){
        Brand brand = brandService.getById(id);
        return Result.build(brand, 200, "成功获取品牌的详细信息");
    }


    @Operation(summary ="删除品牌")
    @DeleteMapping("/{ids}")
    public Result deleteBrand(@PathVariable("ids") @RequestParam List<Long> ids){
        boolean b = brandService.removeBatchByIds(ids);
        return Result.build(b, 200, "成功获取品牌的详细信息");
    }





    @Operation(summary ="分页")
    @GetMapping("/findPage/{page}/{limit}")
    public Result<IPage<Brand>> pageItem(@PathVariable("page") Long page, @PathVariable("limit") long limit) {

        IPage<Brand> page1 = new Page<>(page, limit);
        brandService.queryPage(page1);

        return Result.build(page1, 200, "成功获取品牌的分页数据");
    }

}